package com.gxar.quick.ar.server.base.controller;

import com.gxar.common.entity.Result;
import com.gxar.quick.ar.server.base.common.constant.UserConstant;
import com.gxar.quick.ar.server.base.facade.ExternalMaterialFacade;
import com.gxar.quick.ar.server.base.model.material.info.UploadResultInfo;
import com.gxar.quick.ar.server.base.model.material.vo.request.QueryMaterialListRequest;
import com.gxar.quick.ar.server.base.model.material.vo.request.QuerySketchfabModelsRequest;
import com.gxar.quick.ar.server.base.model.material.vo.request.WordArtToImageRequest;
import com.gxar.quick.ar.server.base.model.material.vo.response.AuthSketchfabResponse;
import com.gxar.quick.ar.server.base.model.material.vo.response.DownloadSketchfabModelResponse;
import com.gxar.quick.ar.server.base.model.material.vo.response.MaterialGifResponse;
import com.gxar.quick.ar.server.base.model.material.vo.response.MaterialListResponse;
import com.gxar.quick.ar.server.base.model.material.vo.response.QuerySketchfabCategoryResponse;
import com.gxar.quick.ar.server.base.model.material.vo.response.QuerySketchfabModelsResponse;
import com.gxar.quick.ar.server.db.service.MaterialService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author nice
 * @version 1.0
 * @created 2023/11/13.
 */
@Api(tags = "素材接口")
@RestController
@RequestMapping("/api/material")
@RequiredArgsConstructor
@Slf4j
public class MaterialController {

    private final ExternalMaterialFacade extMaterialFacade;

    private final MaterialService materialService;

    @ApiOperation("获取sketchfab素材")
    @PostMapping("/sketchfab/query")
    public Result<QuerySketchfabModelsResponse> querySketchfabModels(@RequestBody QuerySketchfabModelsRequest request) {
        QuerySketchfabModelsResponse result = extMaterialFacade.querySketchfabModels(request);
        return Result.success(result);
    }

    @ApiOperation("sketchfab用户授权")
    @GetMapping("/sketchfab/auth")
    public Result<AuthSketchfabResponse> auth(@RequestParam String userName, @RequestParam String password) {
        AuthSketchfabResponse result = extMaterialFacade.generateToken(userName, password);
        return Result.success(result);
    }

    @ApiOperation("下载sketchfab素材")
    @GetMapping("/sketchfab/download")
    public Result<DownloadSketchfabModelResponse> download(@RequestParam String uid, @RequestParam String token) {
        DownloadSketchfabModelResponse result = extMaterialFacade.downloadSketchfabModels(uid, token);
        return Result.success(result);
    }

    @ApiOperation("查询sketchfab模型类目")
    @GetMapping("/sketchfab/category")
    public Result<QuerySketchfabCategoryResponse> querySketchfabCategory() {
        QuerySketchfabCategoryResponse result = extMaterialFacade.querySketchfabCategory();
        return Result.success(result);
    }

    @ApiOperation("分页查询素材信息")
    @PostMapping("/query")
    public Result<List<MaterialListResponse>> list(@RequestBody QueryMaterialListRequest request) {
        List<MaterialListResponse> list = materialService.list(request.getPageNow(), request.getPageSize(),
                request.getType());
        return Result.success(list);
    }

    @ApiOperation("艺术字生成图片")
    @PostMapping("/wordArtToImage")
    public Result<UploadResultInfo> wordArtToImage(HttpServletRequest request,
            @RequestBody WordArtToImageRequest param) {
        String token = request.getHeader(UserConstant.TOKEN_HEADER_NAME);
        String userId = request.getHeader(UserConstant.USER_ID);
        UploadResultInfo result = materialService.textToImage(param.getText(), param.getFontSize(), token, userId,
                param.getId(), param.getFontStyle());
        return Result.success(result);
    }

    @ApiOperation("上传物料")
    @GetMapping("/upload")
    public Result upload(@RequestParam String path, @RequestParam Integer type, @RequestParam Boolean thumbnail) {
        materialService.uploadFiles(path, type, thumbnail);
        return Result.success();
    }

    @ApiOperation("GIF文件转雪碧图")
    @PostMapping("/gifToSprite")
    public Result gifToSprite(@RequestParam(required = false) String path, HttpServletRequest request,
            @RequestParam(name = "file", required = false) MultipartFile file) {
        String token = request.getHeader(UserConstant.TOKEN_HEADER_NAME);
        String userId = request.getHeader(UserConstant.USER_ID);
        MaterialGifResponse response = materialService.gifToSprite(path, token, userId, file);
        return Result.success(response);
    }

    @ApiOperation("重置雪碧图缓存")
    @GetMapping("/resetMaterialResponseMap")
    public Result resetMaterialResponseMap() {
        Integer i = materialService.resetMaterialResponseMap();
        return Result.success(i);
    }
}
